﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Class2HomeworkT2
{
    public partial class Form1 : Form
    {
        string a="";
        static int addNums = 0;
        string upperLimit;
        string lowerLimit;
        public Form1()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            upperLimit = textBox1.Text;
            textBox5.Text = upperLimit+"";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            lowerLimit = textBox2.Text;
            textBox4.Text = lowerLimit + "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int x1 = int.Parse(lowerLimit);
            int x2 = int.Parse(upperLimit);
            if (x1> x2)
            {
                a = "下限大于上限，请重新输入";
            }
            else
            {
                for (int j = x1; j <= x2; j++)
                {
                    checkAndAddPrimeNumber(j);
                }
            }
            label6.Text = a;
        }
        public void checkAndAddPrimeNumber(int x)
        {
            if(x==2||x==3||x==5)//若数为6之前的素数，加入数组a;
            {
                addPrimeNumber(x);
            }
            
            else if(x>6)//x大于6时，仅当x=6n+1或6n+5时才可能为素数，对可能为素数的x进行素数检测，若为真，加入数组
            {
                if(x%6==1||x%6==5)
                {
                    if(checkPrimeNumber(x))
                    {
                        addPrimeNumber(x);
                    }
                }
            }
                  
        }
        public void addPrimeNumber(int x)
        {
            
            addNums++;
            if(addNums%10==0)
            {
                a = a + x + "\r\n";
            }
            else
            {
                a = a + x + " ";
            }

        }
        public bool checkPrimeNumber(int x)//检测一个数是否为素数，依次除被检测数之前的数，观察是否有余数，若余数全不为0，返回值为真，若余数出现0，该数不是素数，返回值为假
        {
            bool isPrimeNumber=true;
            for(int i=2;i<x;i++)
            {
                if(x%i==0)
                {
                    isPrimeNumber = false;
                    break;
                }
            }
            return isPrimeNumber;
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            upperLimit = "";
            lowerLimit = "";
            textBox1.Text = "";
            textBox2.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            a = "";
            addNums = 0;
            label6.Text = "素数输出处";
        }
    }
}
